<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    var templateStr = '<h1>我买了一个{{thing}}，好{{mood}}</h1>'

    var data = {
      thing: '华为手机',
      mood: '开心'
    }

    // console.log('我爱上学，我也爱打游戏'.replace('我','你'))
    // console.log('我爱上学，我也爱打游戏'.replace(/我/g,'你'))
    // console.log('我爱上学，我也爱打游戏'.replace(/我/g, (a, b, c) => {
    //   console.log(a, b, c)
    //   return '你'
    // }))

    // console.log(templateStr.replace(/\{\{(\w+)\}\}/g, (findStr, $1) => {
    //   console.log(findStr, $1)
    //   return data[$1]
    // }))

    function render(templateStr, data) {
      return templateStr.replace(/\{\{(\w+)\}\}/g, function (findStr, $1) {
        return data[$1]
      })
    }

    var result = render(templateStr, data)
    console.log(result)
  </script>
</body>
</html>